package com.chinalife.project.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 文件操作工具类
 * @author synhard
 * @date 2024/7/18 17:07
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
public class FileUtil {

    // 读取文本文件，返回List<String>
    public static List<String> readTextFile(String filePath) throws IOException {
        List<String> lines = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }
        return lines;
    }

    // 写入文本文件，传入List<String>和文件路径
    public static void writeTextFile(List<String> lines, String filePath) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String line : lines) {
                writer.write(line);
                writer.newLine();
            }
        }
    }

    // 读取CSV文件，返回二维字符串数组
    public static List<String[]> readCsvFile(String filePath) throws IOException {
        List<String[]> records = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] values = line.split(",");
                records.add(values);
            }
        }
        return records;
    }

    // 写入CSV文件，传入二维字符串数组和文件路径
    public static void writeCsvFile(List<String[]> data, String filePath) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (String[] line : data) {
                writer.write(String.join(",", line));
                writer.newLine();
            }
        }
    }

    // 读取Excel文件，返回二维字符串数组（仅支持xlsx格式）
    public static List<String[]> readExcelFile(String filePath) throws IOException {
        List<String[]> records = new ArrayList<>();
        try (Workbook workbook = new XSSFWorkbook(new FileInputStream(filePath))) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                List<String> values = new ArrayList<>();
                for (Cell cell : row) {
                    values.add(cell.toString());
                }
                records.add(values.toArray(new String[0]));
            }
        }
        return records;
    }

    // 写入Excel文件，传入二维字符串数组和文件路径（仅支持xlsx格式）
    public static void writeExcelFile(List<String[]> data, String filePath) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet();
            int rowCount = 0;
            for (String[] row : data) {
                Row sheetRow = sheet.createRow(rowCount++);
                int cellCount = 0;
                for (String cell : row) {
                    sheetRow.createCell(cellCount++).setCellValue(cell);
                }
            }
            try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
                workbook.write(outputStream);
            }
        }
    }

    // 读取JSON文件，返回JsonObject
    public static JsonObject readJsonFile(String filePath) throws IOException {
        Gson gson = new Gson();
        try (Reader reader = new FileReader(filePath)) {
            return gson.fromJson(reader, JsonObject.class);
        }
    }

    // 写入JSON文件，传入JsonObject和文件路径
    public static void writeJsonFile(JsonObject jsonObject, String filePath) throws IOException {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        try (Writer writer = new FileWriter(filePath)) {
            gson.toJson(jsonObject, writer);
        }
    }
}
